package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.AtvMainOffline;
import com.shycloud.mido.member.api.entity.AtvMainOfflineMemberDetail;
import com.shycloud.mido.member.api.vo.AtvMainOfflineVO;
import com.shycloud.mido.member.service.AtvMainOfflineMemberDetailService;
import com.shycloud.mido.member.service.AtvMainOfflineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 小程序新活动业务模块
 *
 * @author nianhua.jiang
 * @date 2021/8/23 14:52
 */
@RestController
@AllArgsConstructor
@RequestMapping("/v2/atv")
@Api(tags = "小程序新活动业务模块")
public class AtvMainOfflineController {

	/** 活动模块 service */
	private final AtvMainOfflineService atvMainOfflineService;
	/** 活动详情分享 service */
	private final AtvMainOfflineMemberDetailService atvMainOfflineMemberDetailService;

	/**
	 * 添加活动
	 *
	 * @param atvMainOffline
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2021/8/23 14:53
	 */
	@ApiOperation("创建活动")
	@PostMapping("addAtvMain")
	public R addAtvMain(@RequestBody AtvMainOfflineVO atvMainOffline) {
		//处理图片，逗号分隔存储地址
		if (atvMainOffline.getAtvPicUrlArr() != null) {
			String atvPicUrls = Arrays.asList(atvMainOffline.getAtvPicUrlArr()).stream().collect(Collectors.joining(","));
			atvMainOffline.setAtvPicUrls(atvPicUrls);
		}
		if (atvMainOffline.getOrgPicUrlArr() != null) {
			String orgPicUrls = Arrays.asList(atvMainOffline.getOrgPicUrlArr()).stream().collect(Collectors.joining(","));
			atvMainOffline.setOrgPicUrls(orgPicUrls);
		}
		atvMainOffline.setCreateTime(LocalDateTime.now());
		return new R(atvMainOfflineService.save(atvMainOffline));
	}

	@ApiOperation("删除活动")
	@DeleteMapping("/delAtvMain/{id}")
	public R delAtvMain(@PathVariable String id) {
		return new R(atvMainOfflineService.removeById(id));
	}

	@ApiOperation("编辑活动")
	@PutMapping("/updAtvMain")
	public R updAtvMain(@RequestBody AtvMainOfflineVO atvMainOfflineVO) {

		//处理图片，逗号分隔存储地址
		if (atvMainOfflineVO.getAtvPicUrlArr() != null) {
			String atvPicUrls = Arrays.asList(atvMainOfflineVO.getAtvPicUrlArr()).stream().collect(Collectors.joining(","));
			atvMainOfflineVO.setAtvPicUrls(atvPicUrls);
		}
		if (atvMainOfflineVO.getOrgPicUrlArr() != null) {
			String orgPicUrls = Arrays.asList(atvMainOfflineVO.getOrgPicUrlArr()).stream().collect(Collectors.joining(","));
			atvMainOfflineVO.setOrgPicUrls(orgPicUrls);
		}
		atvMainOfflineVO.setCreateTime(LocalDateTime.now());

		return new R(atvMainOfflineService.updateById(atvMainOfflineVO));
	}

	@ApiOperation("获取活动列表")
	@GetMapping("/getAtvMainList")
	public R getAtvMainList(String memberId) {
		return new R(atvMainOfflineService.getAtvMainList(memberId));
	}

	@ApiOperation("后台管理-获取活动列表（分页）")
	@GetMapping("/getAtvMainListPage")
	public R getAtvMainListPage(Integer current, Integer size, String atvSubject, String status) {
		return new R(atvMainOfflineService.getAtvMainListPage(new Page(current, size), atvSubject, status));
	}

	@ApiOperation("获取活动详情")
	@GetMapping("/getAtvMainDetail")
	public R getAtvMainDetail(String id) {

		AtvMainOffline atvMainOffline = atvMainOfflineService.getById(id);
		AtvMainOfflineVO atvMainOfflineVO = new AtvMainOfflineVO();
		BeanUtils.copyProperties(atvMainOffline, atvMainOfflineVO);
		//循环处理 URL
		String atvPicUrls = atvMainOffline.getAtvPicUrls();
		if (StringUtils.isNotBlank(atvPicUrls)) {
			String[] atvPicUrlArr = atvPicUrls.split(",");
			atvMainOfflineVO.setAtvPicUrlArr(atvPicUrlArr);
		}
		String orgPicUrls = atvMainOffline.getOrgPicUrls();
		if (StringUtils.isNotBlank(orgPicUrls)) {
			String[] orgPicUrlArr = orgPicUrls.split(",");
			atvMainOfflineVO.setOrgPicUrlArr(orgPicUrlArr);
		}
		return new R(atvMainOfflineVO);

	}

	@ApiOperation(value = "新增分享用户", notes = "暂时先不校验重复数据，100% 插入成功")
	@PostMapping("/addShareMember")
	public R addShareMember(@Valid @RequestBody AtvMainOfflineMemberDetail atvMainOfflineMemberDetail) {

		List<AtvMainOfflineMemberDetail> atvMainOfflineMemberDetails = atvMainOfflineMemberDetailService
				.list(new QueryWrapper<AtvMainOfflineMemberDetail>()
						.eq(AtvMainOfflineMemberDetail.COL_NAME, atvMainOfflineMemberDetail.getName())
						.or()
						.eq("phone", atvMainOfflineMemberDetail.getPhone())
						.and(x -> x.eq("main_offline_id", atvMainOfflineMemberDetail.getMainOfflineId())
								.eq("type", atvMainOfflineMemberDetail.getType()))
				);

		if (atvMainOfflineMemberDetails.size() > 0) {
			if (StringUtils.equals("1", atvMainOfflineMemberDetail.getType())) {
				return new R(atvMainOfflineMemberDetails.get(0));
			} else {
				return new R(CommonConstants.FAIL,"该学生已报名",false);
			}
		}

		atvMainOfflineMemberDetail.setCreateTime(LocalDateTime.now());
		atvMainOfflineMemberDetailService.save(atvMainOfflineMemberDetail);
		return new R(atvMainOfflineMemberDetail);
	}

	@ApiOperation("获取分享用户列表")
	@GetMapping("/getShareListParent")
	public R getShareListParent(@Valid AtvMainOfflineMemberDetail atvMainOfflineMemberDetail) {
		if (SecurityUtils.getMember() == null) {
			return new R(CommonConstants.FAIL, "请先登录以获取列表信息", false);
		}
		return new R(atvMainOfflineMemberDetailService.getShareList(atvMainOfflineMemberDetail, SecurityUtils.getMember().getId()));
	}

}
